package cn.net.zhenchan.common.service;

import cn.net.zhenchan.common.MD5Utils;
import cn.net.zhenchan.common.ServiceMessage;
import cn.net.zhenchan.common.ServiceMessageImpl;
import cn.net.zhenchan.enums.MsgStatus;
import cn.net.zhenchan.utils.NetUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseServiceImpl {

	protected final Logger logger = LoggerFactory
			.getLogger(BaseServiceImpl.class);

    public <T> ServiceMessage<T> returnResult(MsgStatus msgStatus, String message, T o)
    {
        ServiceMessage<T> serviceMessage = new ServiceMessageImpl<T>(
                msgStatus, message, o);
        return serviceMessage;
    }

    public <T> ServiceMessage<T> returnResult(MsgStatus msgStatus, String message)
    {
        return this.returnResult(msgStatus, message, null);
    }


    public <T> ServiceMessage<T> returnParamsError(String message)
	{
		ServiceMessage<T> serviceMessage = new ServiceMessageImpl<T>(
				MsgStatus.PARAMS_ERROR, message);
		return serviceMessage;
	}

	public <T> ServiceMessage<T> returnParamsError()
	{
		return this.returnParamsError("Params error!");
	}

	public <T> ServiceMessage<T> returnException(String message)
	{
		ServiceMessage<T> serviceMessage = new ServiceMessageImpl<T>(
				MsgStatus.EXCEPTION, message);
		return serviceMessage;
	}

	public <T> ServiceMessage<T> returnException()
	{
		return this.returnException("Unexpected error");
	}

	public <T> ServiceMessage<T> returnException(Throwable e)
	{
		String message = e.getMessage();
		if(message == null || !"".equals(message.trim()))
			message = "no message";
		String excepionId = MD5Utils.MD5PlusDate(message);
		logger.error("IP:" + NetUtils.getLocalHost() + ", excepionId(" + excepionId + ")", e);
		return this.returnException("IP:" + NetUtils.getLocalHost() + ", excepionId("
				+ excepionId + ")");
	}

	public <T> ServiceMessage<T> returnNoResult(String message)
	{
		ServiceMessage<T> serviceMessage = new ServiceMessageImpl<T>(
				MsgStatus.NO_RESULT, message);
		return serviceMessage;
	}

	public <T> ServiceMessage<T> returnNoResult()
	{
		return this.returnNoResult("Not find result");
	}

	public <E> ServiceMessage<E> returnCorrectResult(String message, E e)
	{
		ServiceMessage<E> serviceMessage = new ServiceMessageImpl<E>(
				MsgStatus.NORMAL, message, e);
		return serviceMessage;
	}

	public <E> ServiceMessage<E> returnCorrectResult(E e)
	{
		return returnCorrectResult(MsgStatus.NORMAL.name(), e);
	}

	public <E> ServiceMessage<E> returnCorrectResult(String message)
	{
		ServiceMessage<E> serviceMessage = new ServiceMessageImpl<E>(
				MsgStatus.NORMAL, message);
		return serviceMessage;
	}
	

}
